home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-03-05 | 12.6 KB | 415 lines |
- /*
- * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
- * modify and redistribute this software in source and binary code form,
- * provided that i) this copyright notice and license appear on all copies of
- * the software; and ii) Licensee does not utilize the software in a manner
- * which is disparaging to Sun.
- *
- * This software is provided "AS IS," without a warranty of any kind. ALL
- * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
- * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
- * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
- * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
- * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
- * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
- * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
- * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
- * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGES.
- *
- * This software is not designed or intended for use in on-line control of
- * aircraft, air traffic, aircraft navigation or aircraft communications; or in
- * the design, construction, operation or maintenance of any nuclear
- * facility. Licensee represents and warrants that it will not use or
- * redistribute the Software for such purposes.
- */
-
-
- import java.lang.*;
- import java.awt.*;
- import java.applet.*;
-
- import java.io.*;
-
- import org.omg.CosNaming.*;
- import org.omg.CosNaming.NamingContextPackage.*;
- import org.omg.CORBA.*;
- import com.sun.CORBA.iiop.*;
-
- // This is the directory where the idl-to-java compiler put stuff
- import PortfolioManager.* ;
-
- public
- class PortfolioApplet
- extends Applet
- {
- /** ORB
- **/
- public
- static org.omg.CORBA.ORB theOrb_;
-
- public
- static PortfolioManager.Portfolio ref_;
-
-
- private Insets mInsets = new Insets(2,2,2,2);
-
- private Label mPortfolio;
- private Label mPrice;
- private TextField mPriceField;
- private Label mQuantity;
- private TextField mQuantityField;
- private Label mTicker;
- private TextField mTickerField;
- private Button mAdd;
- private Button mSell;
- private Button mBuy;
- private Button mDelete;
- private Button mChange;
-
- private Button mReport;
- private TextArea mReportText;
-
- public
- void init() {
- setORBConnection();
- super.init();
- createGui();
- }
-
- /** Initialize ORB connection
- **/
- public
- void setORBConnection() {
- try {
- // create and initialize the ORB
- theOrb_ = org.omg.CORBA.ORB.init(this, null);
- System.out.println("Portfolio Client: Successfull ORB initialization");
-
- // get the root naming context
- org.omg.CORBA.Object objRef =
- theOrb_.resolve_initial_references("NameService");
- NamingContext ncRef = NamingContextHelper.narrow(objRef);
-
- // resolve the Object Reference in Naming
- NameComponent nc = new NameComponent("Portfolio", "");
- NameComponent path[] = {nc};
- ref_ = PortfolioManager.PortfolioHelper.narrow(ncRef.resolve(path));
-
- System.out.println("Portfolio Client: Portfolio accessed");
- }
- catch (Exception ex) {
- ex.printStackTrace();
- System.out.println("Portfolio Client: Exception-> " + ex) ;
- }
- }
-
- public
- void createGui() {
- setBackground(Color.lightGray);
-
- GridBagLayout gb = new GridBagLayout();
- GridBagConstraints gbc = new GridBagConstraints();
- setLayout(gb);
-
- int row = 0;
-
- gbc.anchor = GridBagConstraints.NORTH;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.gridx = 0;
- gbc.gridy = row;
- gbc.gridwidth = 2;
- gbc.gridheight = 1;
- gbc.weightx = 1;
- gbc.weighty = 0;
- mPortfolio = new Label("Portfolio Manager", Label.CENTER);
- mPortfolio.setFont(new Font("Helvetica", Font.BOLD, 16));
- gb.setConstraints(mPortfolio, gbc);
- add(mPortfolio);
-
- row++;
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.gridx = 0;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mTicker = new Label("Ticker", Label.LEFT);
- gb.setConstraints(mTicker, gbc);
- add(mTicker);
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.gridx = 1;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 1;
- gbc.weighty = 0;
- mTickerField = new TextField("Ticker");
- gb.setConstraints(mTickerField, gbc);
- add(mTickerField);
-
- row++;
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.gridx = 0;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mQuantity = new Label("Quantity", Label.LEFT);
- gb.setConstraints(mQuantity, gbc);
- add(mQuantity);
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.gridx = 1;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 1;
- gbc.weighty = 0;
- mQuantityField = new TextField("0");
- gb.setConstraints(mQuantityField, gbc);
- add(mQuantityField);
-
- row++;
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.gridx = 0;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mPrice = new Label("Price", Label.LEFT);
- gb.setConstraints(mPrice, gbc);
- add(mPrice);
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.gridx = 1;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 1;
- gbc.weighty = 0;
- mPriceField = new TextField("0.00");
- gb.setConstraints(mPriceField, gbc);
- add(mPriceField);
-
- row++;
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.NONE;
- gbc.gridx = 0;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mAdd = new Button("Add");
- gb.setConstraints(mAdd, gbc);
- add(mAdd);
-
- gbc.anchor = GridBagConstraints.NORTH;
- gbc.fill = GridBagConstraints.NONE;
- gbc.gridx = 1;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mSell = new Button("Sell");
- gb.setConstraints(mSell, gbc);
- add(mSell);
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.NONE;
- gbc.gridx = 2;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mBuy = new Button("Buy");
- gb.setConstraints(mBuy, gbc);
- add(mBuy);
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.NONE;
- gbc.gridx = 3;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mChange = new Button("Change");
- gb.setConstraints(mChange, gbc);
- add(mChange);
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.NONE;
- gbc.gridx = 4;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mDelete = new Button("Delete");
- gb.setConstraints(mDelete, gbc);
- add(mDelete);
-
- row++;
- row++;
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.NONE;
- gbc.gridx = 0;
- gbc.gridy = row;
- gbc.gridwidth = 1;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mReport = new Button("Report");
- gb.setConstraints(mReport, gbc);
- add(mReport);
-
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.gridx = 1;
- gbc.gridy = row;
- gbc.gridwidth = 4;
- gbc.gridheight = 1;
- gbc.weightx = 0;
- gbc.weighty = 0;
- mReportText = new TextArea(" ", 5, 80);
- gb.setConstraints(mReportText, gbc);
- add(mReportText);
-
-
- }
-
- /**
- * Handle action events from various controls
- * Overrides Component.action().
- */
- public
- boolean action(Event pe, java.lang.Object po) {
- // User selected month from the choice
- try {
- if (pe.target == mAdd) {
- getAppletContext().showStatus("Add " + mTickerField.getText()
- + " to Portfolio at US$" + mPriceField.getText());
- Double price_ = Double.valueOf(mPriceField.getText());
- java.lang.Integer quantity_ =
- java.lang.Integer.valueOf(mQuantityField.getText());
- ref_.addStock(mTickerField.getText(),
- (short)quantity_.intValue(),
- price_.doubleValue());
- updateReport();
- return true;
- }
- if (pe.target == mSell) {
- getAppletContext().showStatus("Sell " + mQuantityField.getText()
- + " shares of " + mTickerField.getText() + " at price US$ "
- + mPriceField.getText());
- java.lang.Integer quantity_ =
- java.lang.Integer.valueOf(mQuantityField.getText());
- ref_.sell(mTickerField.getText(), (short)quantity_.intValue());
- updateReport();
- return true;
- }
- if (pe.target == mBuy) {
- getAppletContext().showStatus("Buy " + mQuantityField.getText()
- + " shares of " + mTickerField.getText() + " at price US$ "
- + mPriceField.getText());
- java.lang.Integer quantity_ =
- java.lang.Integer.valueOf(mQuantityField.getText());
- ref_.buy(mTickerField.getText(), (short)quantity_.intValue());
- updateReport();
- return true;
- }
- if (pe.target == mDelete) {
- getAppletContext().showStatus("Delete " + mTickerField.getText()
- + " from Portfolio");
- ref_.deleteStock(mTickerField.getText());
- updateReport();
- return true;
- }
- if (pe.target == mChange) {
- getAppletContext().showStatus("Change " + mTickerField.getText()
- + " in Portfolio to US$" + mPriceField.getText());
- Double price_ = Double.valueOf(mPriceField.getText());
- java.lang.Integer quantity_ =
- java.lang.Integer.valueOf(mQuantityField.getText());
- ref_.deleteStock(mTickerField.getText());
- ref_.addStock(mTickerField.getText(),
- (short)quantity_.intValue(),
- price_.doubleValue());
- updateReport();
- return true;
- }
- if (pe.target == mReport) {
- updateReport();
- return true;
- }
- return(super.action(pe, po));
- }
- catch(NumberFormatException ex) {
- System.out.println("Exception: " + ex);
- }
- catch(Exception ex) {
- ex.printStackTrace();
- System.out.println("Exception: " + ex);
- }
- return true;
- }
-
- void updateReport() {
- mReportText.replaceText(ref_.report(), 0,
- mReportText.getText().length());
- mReportText.appendText("\n-----------------------------\n");
- mReportText.appendText("Total Value: US $" + new Double(ref_.value()) + "\n");
-
- }
-
-
- /**
- * Paint nice raized 3D background.
- * Overrides Component.paint().
- */
- public
- void
- paint(Graphics pg)
- {
- pg.setColor(Color.black);
- pg.drawLine(1, 0, bounds().width-2, 0);
- pg.drawLine(0, 1, 0, bounds().height-2);
- pg.drawLine(1, bounds().height-1, bounds().width - 2, bounds().height - 1);
- pg.drawLine(bounds().width - 1, 1, bounds().width - 1, bounds().height - 2);
-
- pg.setColor(getBackground());
- //pg.draw3DRect(1, 1, bounds().width - 3, bounds().height - 3, true);
- pg.fill3DRect(1, 1, bounds().width - 2, bounds().height - 2, true);
- }
-
- /**
- * For better looks, let Layout Manager leave some margin.
- */
- public
- Insets
- insets()
- {
- return mInsets;
- }
- }
-